import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/message/message_config.dart';

import '../../model/food/food_record_model.dart';

class BatchCancelFoodViewModel extends BaseViewModel {
  final int? breakfastId;
  final int? lunchId;
  final int? dinnerId;
  final String? date;

  var model = FoodRecordModel();

  var selectId = <int>[];

  BatchCancelFoodViewModel(BuildContext context,
      {this.breakfastId, this.lunchId, this.dinnerId, this.date})
      : super(context);

  Widget contentView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            CommonText('取消订餐', textSize: 18),
            ElevatedButton(onPressed: () {
              submitCancel();
            }, child: Text('确定'))
          ],
        ),
        Container(
          height: 15,
        ),
        CommonText(date),
        if (breakfastId != null) ...[
          Container(
            height: 15,
          ),
          CheckWithText(
            title: "取消早餐",
            onChanged: (value) {
              if (value) {
                selectId.add(breakfastId!);
              } else {
                selectId.remove(breakfastId);
              }
            },
            defaultSelected: selectId.contains(breakfastId),
          ),
        ],
        if (lunchId != null) ...[
          Container(
            height: 10,
          ),
          CheckWithText(
            title: "取消午餐",
            onChanged: (value) {
              if (value) {
                selectId.add(lunchId!);
              } else {
                selectId.remove(lunchId);
              }
            },
            defaultSelected: selectId.contains(lunchId),
          ),
        ],
        if (dinnerId != null) ...[
          Container(
            height: 10,
          ),
          CheckWithText(
            title: "取消晚餐",
            onChanged: (value) {
              if (value) {
                selectId.add(dinnerId!);
              } else {
                selectId.remove(dinnerId);
              }
            },
            defaultSelected: selectId.contains(dinnerId),
          )
        ]
      ],
    ).addToContainer(
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
    );
  }

  Future<void> submitCancel() async {
    if (selectId.isEmpty) {
      showToast("请选择要需要的餐段");
      return;
    }
    showLoadingDialog(msg: '提交中...');
    try {
      for (var id in selectId) {
        await model.cancelFood(id);
      }
      sendMessage(SendMessageEvent(MessageConfig.PAY_FOOD_SUCCESS));
      hideDialog();
      finish();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }
}
